﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HZ.DrugTrade.BLLFactory;
using HZ.DrugTrade.IBLL;
using HZ.DrugTrade.Model;
using HZ.DrugTrade.Model.Enum;

namespace HZ.DrugTrade.Web.Admin.DrugLibManage
{
    public partial class HospitalLoginAllGoods : BasePage
    {
        public HospitalLoginAllGoods()
        {
            this.ModuleNo = Module.采购项目管理.ToString();//权限
            this.PageOperateType = OperateType.查看产品填报.ToString();
        }
        IBLL_HospitalLoginPriceGoods ih = Factory.BLL_HospitalLoginPriceGoods();
        public string strGoodsID;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                bindArea();
                bindHospital();
                Bind();

            }
        }

        /// <summary>
        /// 绑定符合条件的采购明细
        /// </summary>
        private void Bind()
        {

            int totalCount;
            IList<Model.View.HospitalLoginPriceGoods> purchaseOrderDetailList = ih.GetGoodsByWhere(GetWhere(), AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize, out totalCount);
            AspNetPager1.RecordCount = totalCount;
            gvwNoConfirm.PageSize = AspNetPager1.PageSize;
            gvwNoConfirm.DataSource = purchaseOrderDetailList;
            gvwNoConfirm.DataBind();
        }

        public Model.Query.HospitalLoginPriceGoods GetWhere()
        {
            Model.Query.HospitalLoginPriceGoods where = new HZ.DrugTrade.Model.Query.HospitalLoginPriceGoods();
            if (txtGoodsID.Text != "")
            {
                if (Fun.IsInt(txtGoodsID.Text.Trim()))
                {
                    where.GoodsID = txtGoodsID.Text.Trim();//流水号

                }
                else
                {
                    HZ.Web.WebUtil.Alert("请输入整数编号！");

                }
            }
            where.Material = txtMaterial.Text.Trim(); //材质
            where.GoodsName = txtGoodsName.Text.Trim();//产品名称
            where.Outlookc = txtOutLook.Text.Trim();
            where.CompanyName_SC = txtCompanyName_Sc.Text.Trim();//生产企业
            where.SortFields = " price asc";

            string strWhere = "";
            if (ddl_area.Text == "全部")
            {
                //strWhere = " goodsstate=2 and";
            }
            else
            {
                //strWhere = "  goodsstate=2 and areaid like'" + ddl_area.SelectedValue + "%' and";
                strWhere = "  areaid like'" + ddl_area.SelectedValue + "%' and";
            }

            if (ddl_hospitalname.Text == "全部")
            {
                strWhere += " ";
            }
            else
            {
                strWhere += "  HospitalName like'%" + ddl_hospitalname.Text + "%' and";
            }

            where.Append = strWhere.Substring(0, strWhere.Length - 4);
            return where;
        }
        /// <summary>
        /// 绑定地区
        /// </summary>
        private void bindArea()
        {
            //            string sql = string.Format(@"SELECT distinct AreaName,left(TB_Area.AreaID,4)AreaID FROM [View_HospitalLoginPrice] inner  join TB_Area on
            //                                        left([View_HospitalLoginPrice].AreaID,4)+'00'=TB_Area.AreaID Where  goodsstate=2 ");
            string sql = string.Format(@"SELECT distinct AreaName,SUBSTR(TB_Area.AreaID,4)AreaID FROM View_HospitalLoginPrice inner  join TB_Area on
                                        SUBSTR(View_HospitalLoginPrice.AreaID,4)||'00'=TB_Area.AreaID ");
            System.Data.DataTable dt = HZ.Data.DbHelperFactory.Create().ExecuteTable(sql);
            System.Data.DataRow row = dt.NewRow();
            row[0] = "全部";
            dt.Rows.InsertAt(row, 0);
            ddl_area.DataSource = dt;
            ddl_area.SelectedIndex = 0;
            ddl_area.DataTextField = "AreaName";
            ddl_area.DataValueField = "AreaID";
            ddl_area.DataBind();
            dt.Dispose();
        }
        /// <summary>
        /// 绑定医疗机构
        /// </summary>
        private void bindHospital()
        {
            //string sql = string.Format(@"SELECT distinct HospitalName FROM [View_HospitalLoginPrice]  Where goodsstate=2 ");
            string sql = string.Format(@"SELECT distinct HospitalName FROM View_HospitalLoginPrice  ");
            System.Data.DataTable dt1 = HZ.Data.DbHelperFactory.Create().ExecuteTable(sql);
            System.Data.DataRow row = dt1.NewRow();
            row[0] = "全部";
            dt1.Rows.InsertAt(row, 0);
            ddl_hospitalname.DataSource = dt1;
            ddl_hospitalname.SelectedIndex = 0;
            ddl_hospitalname.DataTextField = "HospitalName";
            ddl_hospitalname.DataValueField = "HospitalName";
            ddl_hospitalname.DataBind();
            dt1.Dispose();
        }
        /// <summary>
        /// 翻页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void AspNetPager1_OnPageChanged(object sender, EventArgs e)
        {
            Bind();
        }


        /// <summary>
        /// 导出
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnExport_Click(object sender, EventArgs e)
        {
            //int totalCount;
            //IList<Model.View.HospitalLoginPriceGoods> purchaseOrderDetailList = ih.GetGoodsByWhere(GetWhere(), 1, 0, out totalCount);
            //System.Data.DataTable tb = HZ.Data.ModelConvert.GetDataTableFromModel(purchaseOrderDetailList);
            //HZ.Excel.ExcelHelper.ExportDataForWeb(tb, gvwNoConfirm);
            HZ.DrugTrade.Model.Query.HospitalLoginPriceGoods hospitalLoginPriceGoods = GetWhere();
            string where = HZ.DrugTrade.DAL2.DAL_Helper.GetWhereCondition<HZ.DrugTrade.Model.Query.HospitalLoginPriceGoods>(hospitalLoginPriceGoods, true);
            string sql = string.Format(@"SELECT GoodsID 产品编号,GoodsName 产品名称,Outlookc 规格型号,Material 材质,Unit 最小计量单位,PackUnit 包装单位,
                                         CompanyName_SC 生产企业,HospitalName 医疗机构名称,HospitalID 医疗机构编号,Price 医疗机构价格,CountProduces 医疗机构采购量 FROM View_HospitalLoginPrice WHERE 1=1 {0}", where);
            HZ.DrugTrade.BLL.ExcelHelper.ExportByWeb(HZ.Data.DbHelperFactory.Create().ExecuteTable(sql), "", "医疗机构确认价格汇总.xls");
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnQuery_Click(object sender, EventArgs e)
        {
            AspNetPager1.CurrentPageIndex = 1;
            //Bind();
        }



    }
}
